//package com.xiaotao.test01.service;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.scheduling.annotation.SchedulingConfigurer;
//import org.springframework.scheduling.config.ScheduledTaskRegistrar;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.ScheduledFuture;
//import java.util.concurrent.atomic.AtomicInteger;
//
///**
// * @author xiaotao
// */
//@Component
//@Slf4j
//public class DynamicSchedulerService {
//
//    @Resource
//    private ScheduledTaskRegistrar scheduledTaskRegistrar;
//    @Resource
//    private WebSocketServer webSocketServer;
//
//    private final Map<String, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
//
//    /**
//     *
//     * @param taskName 发送的信息
//     * @param message 发送的信息
//     * @param minute 总共时长 秒
//     * @param percentage 占比
//     */
//    public void sendMessageScheduledTask(String taskName, String message, Integer minute, Integer percentage,String uId) {
//        Integer total = minute * 1000;
//        long frequency = total / percentage;
//        AtomicInteger count = new AtomicInteger(1);
//        // 创建定时任务
//        ScheduledFuture<?> scheduledFuture = scheduledTaskRegistrar.getScheduler().scheduleAtFixedRate(() -> {
//            if (count.get() >= percentage) {
//                log.debug("消息发送线程执行完毕");
//                cancelScheduledTask(taskName);
//            }else {
//                webSocketServer.sendMessage("sss", uId);
//                log.error("发送消息 ：" + message + " 计数:" + count + '%');
//                String name = Thread.currentThread().getName();
//                System.out.println(name + "发送消息 ：" + message + " 计数:" + count + '%');
//            }
//            count.incrementAndGet();
//        }, frequency);
//        scheduledTasks.put(taskName, scheduledFuture);
//    }
//
//    public void cancelScheduledTask(String taskName) {
//        // 取消定时任务
//        ScheduledFuture<?> scheduledFuture = scheduledTasks.get(taskName);
//        if (scheduledFuture != null) {
//            scheduledFuture.cancel(true);
//            scheduledTasks.remove(taskName);
//        }
//    }
//}
